<?php

class Template_model extends CI_Model {
    /*
     * 添加新模板分类
     */

    public function addType($data) {
        $query = $this->db->insert('su_template_type', $data);
        return $this->db->affected_rows() ? true : false;
    }

    /*
     * 获取模板分类数据
     */

    public function getTypeList($department_id, $order = 1) {
        $where = [
            'department_id' => $department_id
        ];
        $order = $order != 1 ? 'sort DESC,id DESC' : 'id DESC';
        $query = $this->db->select('*')->where($where)->order_by($order)->get('su_template_type');
        return $query->result_array();
    }

    /*
     * 删除模板分类数据
     */

    public function delType($id) {
        $where = [
            'id' => $id
        ];
        $this->db->where($where)->delete('su_template_type');
        return $this->db->affected_rows() ? true : false;
    }

    /*
     * 修改模板分类数据
     */

    public function updateType($data) {
        $where = [
            'id' => $data['id']
        ];
        unset($data['id']);
        $this->db->where($where)->update('su_template_type', $data);
        return $this->db->affected_rows() ? true : false;
    }

    /*
     * 添加新模板
     */

    public function addTemplate($data) {
        $query = $this->db->insert('su_template', $data);
        return $this->db->affected_rows() ? true : false;
    }

    
    public function getTemplate($where, $order = 1) {
        $order = $order != 1 ? 'sort DESC,id DESC' : 'id DESC';
        $query = $this->db->select('*')->where($where)->order_by($order)->get('su_template');
        return $query->result_array();
    }
    
    public function getTemplateByid($where) {
        $query = $this->db->select('*')->where($where)->get('su_template');
        return $query->row_array();
    }
    
    
    /*
     * 获取模板列表
     * CI中的limit语法limit(a,b);
     * a代表长度
     * b代表分页
     */

    public function getTemplateList($page, $limit, $department_id, $order = 1) {
        $where = [
            'su_template.department_id' => $department_id
        ];
        $order = $order != 1 ? 'sort DESC' : 'id DESC';
        $query = $this->db->select('*')->where($where)->order_by($order)->limit($limit, $page)->get('su_template');
        return $query->result_array();
    }

    /*
     * 获取记录总数
     */

    public function getSum($table,$where) {
        $query = $this->db->select('id')->where($where)->get($table);
        return count($query->result_array());
    }

    /*
     * 删除模板
     */

    public function delTemplateList($data) {
        $state = $this->db->where_in('id', $data)->delete('su_template');
        return $state;
    }

    /*
     * 修改信息
     */

    public function changeTemplateType($data, $where) {
        $state = $this->db->where_in('id', $where)->update('su_template', $data);
        return $state;
    }

}
